package wonderful.com.oneminute.activity;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import wonderful.com.oneminute.R;
import wonderful.com.oneminute.adapter.RecondAdapter;

public class RecordActivity extends Activity {

    //定义一个数组放置录音文件
    ArrayList<File> datas = new ArrayList<File>();

    private MediaRecorder mediaRecorder;

    private String path;
    private File[] files;
    private ArrayAdapter<File> adapter;

    Handler hand = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            adapter.notifyDataSetChanged();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        //音频管理器
        mediaRecorder = new MediaRecorder();
        //数据源准备
        initData();
        //初始化
        initView();
    }

    private void initData() {

        File f = new File("/sdcard/sub");
        files = f.listFiles();
        //将数组中的元素转移到集合中，以便适配器的使用
        for (int i = 0; i < files.length; i++) {
            datas.add(files[i]);
        }

    }

    private void initView() {

        //初始化ListView
        ListView listv = (ListView) findViewById(R.id.recordListView);
        //设置监听
        listv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String s = adapter.getItem(position).toString();
                MediaPlayer mp = new MediaPlayer();
                try {
                    mp.setDataSource(s);
                    mp.prepare();
                    mp.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        //适配器准备
        adapter = new ArrayAdapter<File>(getApplicationContext(), android.R.layout.simple_list_item_1, datas);
        //设置适配器
        listv.setAdapter(adapter);
        //初始化录音按钮
        final Button bt1 = (Button) findViewById(R.id.button1);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bt1.getText().equals("开始")) {
                    //得到当前时间，用作录音命名
                    Date date = new Date();
                    //设置音频来源
                    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    //设置格式
                    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    //设置编码
                    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                    //存储路径
                    path = "/sdcard/sub/OneMinute" + date + ".mp3";
                    //设置路径
                    mediaRecorder.setOutputFile(path);
                    try {
                        //准备开始
                        mediaRecorder.prepare();
                        mediaRecorder.start();

                        Toast.makeText(getApplicationContext(), "开始录音", Toast.LENGTH_SHORT).show();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    bt1.setText("停止");
                    bt1.setBackgroundResource(R.drawable.record_running);

                } else if (bt1.getText().equals("停止")) {

                    mediaRecorder.stop();
                    datas.clear();
                    initData();
                    hand.sendEmptyMessage(0);
                    Toast.makeText(getApplicationContext(), "录音完成", Toast.LENGTH_SHORT).show();
                    bt1.setText("开始");
                    bt1.setBackgroundResource(R.drawable.record_stop);
                }
            }
        });

    }


}
